from django.http import HttpResponse, HttpResponseRedirect, JsonResponse
from rest_framework.response import Response
from rest_framework.views import APIView
from myroad.myserializers import *
from myroad.models import *
from myapp.models import Comment
from myapp.myser import CommentSer




class MyroadShow(APIView):
    def get(self,request):
        good=MyroadIndent.objects.all()
        goodser=MyroadIndentSer(good,many=True)
        return Response({'code':200,'message':goodser.data})

class MyroadLogin(APIView):
    def get(self, request):
        username = request.GET.get('username')
        password = request.GET.get('password')
        #查询数据
        user = MyroadUser.objects.filter(username=username, password=password).first()
        if user:
            return Response({'code': 200, 'msg': '登录成功', 'username': user.username, 'uid': user.id})
        else:
            return Response({'msg': '账号或密码错误'})

    def post(self,request):
        data=request.data
        print(data)
        ser=MyroadUserSer(data=data)
        if ser.is_valid():
            ser.save()

            return Response({'msg':20})

class MyroadAddc(APIView):
    def post(self,request):
        data=request.data

        # print(data['gid'])

        ser=MyroadCommentSer(data=data)
        if ser.is_valid():
            # content.num+=1
            ser.save()

            return Response({'msg':'评论成功'})



class MyroadshowCom(APIView):
    def get(self,request):
        gid=request.GET.get('gid')
        print(gid)
        comment=MyroadComment.objects.filter(gid=gid)
        ser=CommentSer(comment,many=True)
        return Response({'message':ser.data})


class Myroadshowcg(APIView):
    def get(self,request):
        gid=request.GET.get('gid')
        #查询单个订单详情
        indent=MyroadIndent.objects.filter(id=gid).first()
        #序列号
        ser=MyroadIndentSer(indent)
        #根据商品gid获取指定商品所有评论
        content=MyroadComment.objects.filter(gid=gid)
        #序列化
        contentser=MyroadCommentSer(content,many=True)

        return Response({'msg':ser.data,"msg2":contentser.data})






